﻿unit cMeowUtils_dy;
{
   _ 2011

   一个简单的 Delphi 复用DLL  目前只是做了些简单的复用功能
   前身只是一个读取磁盘信息的
   以后有空一些可能写复用从C 会增加到搞DLL中
   v 1.0

   作者: Icy
   Web: icy6.com
   vx: icysoft
   E-Mail: icy2010@hotmail.com
   Delphi 上海交流QQ群  118195948

   /*  此单元 动态调用 定义 */

   _ 2018
}
interface
  uses
    Winapi.Windows,
    System.Classes,
    cMeow_de,
    System.SysUtils;
{-----------------------------------------------------------------------------}
const  CompilerDLL = 1;
{$IF CompilerDLL = 1}
   {$DEFINE C_DLL_R}
{$ENDIF}

type
cMeow_dy = record
private
  cMeowHandle:  THandle;
  cMemoryLoad:  Boolean;
  //
  PGetSignatureName: function (const FileName:  PWideChar):PWideChar;stdcall;
  PNowDateTime: procedure (var Dt:  cDateTime);stdcall;
  PZwDeleteFIle: function (const FileName: PWideChar): Boolean;stdcall;
  PFindProcess: function (const Process: PWideChar):DWORD;stdcall;
  PKillProcessById: function (const PID:  DWORD):Boolean;stdcall;
  PReadFileSize: function (const FileName: PWideChar):DWORD;stdcall;
  PCreateSearch: function :cSearch;stdcall;
  PIsInclusionDigit: function (const S: PWideChar):Boolean;stdcall;
  PReadHardDriveInfo: procedure (var HDI: THardDriveInfo);stdcall;
  PCreateProcessInstallLibrary:  function:cProcessInstallLibrary;stdcall;
  PIs64BitPorcess: function (const PID:  DWORD):Boolean;stdcall;
  PIs64BitOS: function:Boolean; stdcall;
  PCreateService: function:cService;stdcall;
  PCreateMonitor: function:cMonitor;stdcall;
  {$IFDEF CPUX64}
  PCreateHook64: function:cHook64;stdcall;
  {$ENDIF}
  PCreateDownloadFile: function:cDownloadFile;stdcall;
  ///
  function GetCMeowProcAddress(const ProName: PAnsiChar):Pointer;
  procedure SetProcAddress;
  function GetLoaded: Boolean;
  ///
public
  function cMeowLoad(const FileName: string):Boolean;overload;
  function cMeowLoad(Stream: TCustomMemoryStream):Boolean;overload;
  {$IFDEF CPUX64}
  function cMeowLoad(const ResName: string = 'CMEOWUTILS64'; const ResType:   string = 'MODULE'):Boolean;overload;
  {$ELSE}
  function cMeowLoad(const ResName: string = 'CMEOWUTILS32'; const ResType:   string = 'MODULE'):Boolean;overload;
  {$ENDIF}
  function cMeowFree:Boolean;
  function cGetSignatureName(const FileName:  PWideChar):PWideChar;
  procedure cNowDateTime(var Dt:  cDateTime);
  function cZwDeleteFIle(const FileName: PWideChar): Boolean;
  function cFindProcess(const Process: PWideChar):DWORD;
  function cKillProcessById(const PID:  DWORD):Boolean;
  function cReadFileSize(const FileName: PWideChar):DWORD;
  function cCreateSearch:cSearch;
  function cIsInclusionDigit(const S: PWideChar):Boolean;
  procedure cReadHardDriveInfo(var HDI: THardDriveInfo);
  function cCreateProcessInstallLibrary:cProcessInstallLibrary;
  function cIs64BitPorcess(const PID:  DWORD):Boolean;
  function cIs64BitOS:Boolean;
  function cCreateService:cService;
  function cCreateMonitor:cMonitor;
  property cLoaded: Boolean read GetLoaded;
  {$IFDEF CPUX64}
  function cCreateHook64:cHook64;
  {$ENDIF}
  function cCreateDownloadFile:cDownloadFile;
end;

implementation
 uses
   Night.Windows.MemoryLib;

{$IFDEF C_DLL_R}
  {$IFDEF CPUX64}
    {$R cMeowUtils64}
  {$ELSE}
    {$R cMeowUtils32}
  {$ENDIF}
{$ENDIF}
{-----------------------------------------------------------------------------}
procedure cMeow_dy.SetProcAddress;
begin
  if  (cMeowHandle > 0) then
  begin
    @PGetSignatureName:= GetCMeowProcAddress('GetSignatureName');
    @PNowDateTime:= GetCMeowProcAddress('NowDateTime');
    @PZwDeleteFIle:= GetCMeowProcAddress('ZwDeleteFIle');
    @PFindProcess:= GetCMeowProcAddress('FindProcess');
    @PKillProcessById:= GetCMeowProcAddress('KillProcessById');
    @PReadFileSize:= GetCMeowProcAddress('ReadFileSize');
    @PCreateSearch:= GetCMeowProcAddress('CreateSearch');
    @PIsInclusionDigit:= GetCMeowProcAddress('IsInclusionDigit');
    @PReadHardDriveInfo:= GetCMeowProcAddress('ReadHardDriveInfo');
    @PCreateProcessInstallLibrary:= GetCMeowProcAddress('CreateProcessInstallLibrary');
    @PIs64BitOS:= GetCMeowProcAddress('Is64BitOS');
    @PIs64BitPorcess:= GetCMeowProcAddress('Is64BitPorcess');
    @PCreateService:= GetCMeowProcAddress('_CreateService');
    @PCreateMonitor:= GetCMeowProcAddress('CreateMonitor');
    {$IFDEF CPUX64}
    @PCreateHook64:= GetCMeowProcAddress('CreateHook64');
    {$ENDIF}
    @PCreateDownloadFile:=GetCMeowProcAddress('CreateDownloadFile');
  end;
end;

function cMeow_dy.GetLoaded: Boolean;
begin
  Result:= ( cMeowHandle > 0 );
end;

function cMeow_dy.cMeowLoad(const FileName: string):Boolean;
begin
  cMeowHandle:=  LoadLibrary(PChar(FileName));
  Result:= (cMeowHandle > 0);
  cMemoryLoad:= False;
  SetProcAddress;
end;

function cMeow_dy.cMeowLoad(Stream: TCustomMemoryStream):Boolean;
begin
  cMeowHandle:= MemoryLoadLibrary(Stream.Memory);
  Result:= (cMeowHandle > 0);
  cMemoryLoad:= True;
  SetProcAddress;
end;

{$IFDEF CPUX64}
function cMeow_dy.cMeowLoad(const ResName: string = 'CMEOWUTILS64'; const ResType:   string = 'MODULE'):Boolean;
{$ELSE}
function cMeow_dy.cMeowLoad(const ResName: string = 'CMEOWUTILS32'; const ResType:   string = 'MODULE'):Boolean;
{$ENDIF}
var
  R:  TResourceStream;
begin
  R:= TResourceStream.Create(HInstance,ResName,PChar(ResType));
  try
    Result:= cMeowLoad(R);
  finally
    R.Free;
  end;
end;

function cMeow_dy.GetCMeowProcAddress(const ProName: PAnsiChar):Pointer;
begin
  if cMemoryLoad then
    Result:= MemoryGetProcAddress(cMeowHandle,ProName)
  else
  Result:= GetProcAddress(cMeowHandle,ProName)
end;

function cMeow_dy.cMeowFree:Boolean;
begin
  if cMeowHandle = 0 then  Exit(False);

  if cMemoryLoad then
     Result:= MemoryFreeLibrary(cMeowHandle)
  else
  Result:= FreeLibrary(cMeowHandle);
end;

function cMeow_dy.cGetSignatureName(const FileName:  PWideChar):PWideChar;
begin
  Result:=  PGetSignatureName(FileName);
end;

procedure cMeow_dy.cNowDateTime(var Dt:  cDateTime);
begin
  PNowDateTime(dt);
end;

function cMeow_dy.cZwDeleteFIle(const FileName: PWideChar): Boolean;
begin
   Result:=  PZwDeleteFIle(filename);
end;

function cMeow_dy.cFindProcess(const Process: PWideChar):DWORD;
begin
  Result:=  PFindProcess(process);
end;

function cMeow_dy.cKillProcessById(const PID:  DWORD):Boolean;
begin
  Result:=  PKillProcessById(pid);
end;

function cMeow_dy.cReadFileSize(const FileName: PWideChar):DWORD;
begin
   Result:=  PReadFileSize(FileName);
end;

function cMeow_dy.cCreateSearch:cSearch;
begin
  Result:=  PCreateSearch;
end;

function cMeow_dy.cIsInclusionDigit(const S: PWideChar):Boolean;
begin
  Result:=  PIsInclusionDigit(S);
end;

procedure cMeow_dy.cReadHardDriveInfo(var HDI: THardDriveInfo);
begin
  PReadHardDriveInfo(hdi);
end;

function cMeow_dy.cCreateProcessInstallLibrary:cProcessInstallLibrary;
begin
  Result:=  PCreateProcessInstallLibrary;
end;

function cMeow_dy.cIs64BitPorcess(const PID:  DWORD):Boolean;
begin
  Result:= PIs64BitPorcess(pid);
end;

function cMeow_dy.cIs64BitOS:Boolean;
begin
  Result:= PIs64BitOS;
end;

function cMeow_dy.cCreateService:cService;
begin
  Result:= PCreateService;
end;

function cMeow_dy.cCreateMonitor:cMonitor;
begin
  Result:= PCreateMonitor;
end;

{$IFDEF CPUX64}
function cMeow_dy.cCreateHook64:cHook64;
begin
  Result:= PCreateHook64;
end;
{$ENDIF}

function cMeow_dy.cCreateDownloadFile:cDownloadFile;
begin
  Result:= PCreateDownloadFile;
end;

end.
